import $API from '@/api/services'

const krpano = {
  state: {
    hotspots: {}
  },
  mutations: {
    SAVE_HOTSPOTS: (state, obj) => {
      state.hotspots[obj.id] = obj.data
    },
    ADD_NEW_COMMENT: (state, data) => {
      let sceneId = data.scene_id
      let comments
      if(state.hotspots[sceneId]) {
        comments = state.hotspots[sceneId].commentlist
        comments.push(data)
      }
    }
  },
  actions: {
    fetchSceneHotspots({ commit }, sceneId) {
      $API('getHotPoint', [sceneId]).then(res => {
        if(res && res.data) {
          const data = res.data
          // const data = {
          //   hotspotlist: res.data.hotspot_list || [],
          //   commentlist: res.data.comment_list || [],
          //   barrage_list: res.data.barrage_list || [],
          // }
          commit('SAVE_HOTSPOTS', {id: sceneId, data: data})
          $VM.$krp.loadHotspots({id: sceneId, data: data})
        }
      })
    },
    addComment({ commit }, opts) {
      $API('addSay', [opts]).then(res => {
        if(res.data) {
          commit('ADD_NEW_COMMENT', res.data)
          this._vm.$vgo.tip('提交成功', 'success')
        }
      }).then(() => {
        this._vm.$krp.showComments()
      })
    }
  }
}

export default krpano
